﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FerryDev.Excel;
using Los.Core;

namespace StudentImporter
{
    class Importer
    {
        static public IEnumerable<Relation> FromXls(string filename)
        {
            CellValues values = CellValues.FromXls(filename, 1);
            string course = "";
            for (int i = 0; i < values.RowCount; i++)
            {
                string[] columns = values.GetRow(i);
                if (IsRecord(columns))
                {
                    yield return Relation.Import(columns, "Course=" + course);
                }
                else
                {
                    string s = string.Concat(columns).Trim();
                    if ((s != "") && (!s.StartsWith("No")))
                    {
                        course = s;
                    }
                }
            }
        }


        static private bool IsRecord(string[] row)
        {
            int dummy;
            return (row.Length > 2) && (row[1] != "") && (Int32.TryParse(row[0], out dummy));
        }
    }
}
